import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../common/routers/routes.dart';
import 'index.dart';

class FaceRegistrationPage extends GetView<FaceRegistrationController> {
  const FaceRegistrationPage({Key? key}) : super(key: key);

  static void open() {
    Get.toNamed(O2OARoutes.commonFaceRegistrationPage);
  }

  // 主视图
  Widget _buildView(BuildContext context) {
    return Obx(() => controller.state.faceList.isEmpty
        ? _noResultView(context)
        : _faceListView());
  }

  Widget _noResultView(BuildContext context) {
    return Center(
      child: Column(
        children: [
     Text('no_more_data'.tr,
          textAlign: TextAlign.center,
          style: Theme.of(context).textTheme.bodySmall),
          const SizedBox(height: 10),
          Padding(
                    padding: const EdgeInsets.only(bottom: 48),
                    child: ElevatedButton(
                        onPressed: () async {
                          await controller.captureAndUploadPhoto();
                        },
                        child: Text('common_ali_face_registration_btn'.tr)))
        ],
      ),
    );
  }

  // 人脸列表
  Widget _faceListView() {
    return ListView.builder(
        itemCount: controller.state.faceList.length,
        itemBuilder: (context, index) {
          final face = controller.state.faceList[index];
          return Padding(
              padding: const EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Image.network(controller.faceUrl(face.id!), headers: controller.headers,
                      width: 180, height: 320),
                  const SizedBox(width: 10),
                  Text('注册时间：${face.createTime?.substring(0, 10) ?? ''}', style: Theme.of(context).textTheme.bodySmall)
                ],
              ));
        });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FaceRegistrationController>(
      builder: (_) {
        return Scaffold(
          appBar:
              AppBar(title: Text("attendance_setting_face_registration".tr)),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
